<template>
  <div class="company">
    <!--  tab拦
            el-tabs
                  v-model:默认选中第一项
                          他的值是对应的子项 -->
    <el-tabs>
      <el-tab-pane label="角色管理">
        <el-button type="primary" @click="addRole">新增角色</el-button>
        <el-table :data="list" border style="margin-top:30px">
          <el-table-column type="index" width="80" label="序号" />
          <el-table-column label="角色" prop="name" />
          <el-table-column label="描述" prop="description" />
          <el-table-column label="操作">
            <template v-slot="{ row }">
              <div>
                <el-button
                  type="success"
                  size="small"
                  @click="assignClick(row.id)"
                  >分配权限</el-button
                >
                <el-button type="primary" size="small" @click="esit(row)"
                  >编辑</el-button
                >
                <el-button type="danger" size="small" @click="jsEs(row)"
                  >删除</el-button
                >
              </div>
            </template>
          </el-table-column>
        </el-table>
        <div class="page">
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="page.page"
            :page-sizes="[1, 2, 5, 10, 20, 100, 200, 300, 400]"
            :page-size="page.pagesize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="page.total"
          >
          </el-pagination>
        </div>
      </el-tab-pane>
      <el-tab-pane label="公司信息" lazy>
        <Info />
      </el-tab-pane>
    </el-tabs>
    <Add ref="add" :is-show.sync="show" @getData="getData" />
    <SysPermissIon :is-show.sync="showSet" ref="set" />
  </div>
</template>
<script>
import { sysRole, sysRoleStae } from '@/api/setting'
import jsCookie from 'js-cookie'
import Add from './components/add'
import Info from './components/info'
import SysPermissIon from './components/sysPermissIonPut.vue'
export default {
  components: {
    Add,
    Info,
    SysPermissIon
  },
  data () {
    return {
      showSet: false,
      mode: 'add',
      show: false,
      list: [],
      page: {
        page: 1,
        //  初始值读取浏览器
        pagesize: +jsCookie.get('pagesize') || 1,
        total: 100
      }
    }
  },
  created () {
    this.getData()
  },
  methods: {
    //  页容量改变
    handleSizeChange (size) {
      console.log(size)
      this.page.pagesize = size
      this.getData()
      jsCookie.set('setting-pagesize', size)
    },
    handleCurrentChange (page) {
      console.log(page, this.page)
      this.page.page = page
      this.getData()
    },
    async getData () {
      const res = await sysRole(this.page)
      this.list = res.data.data.rows
      this.page.total = res.data.data.total
      console.log('角色 列表', res)
    },
    //  新增角色点击
    addRole () {
      this.show = true
      this.$refs.add.mode = 'add'
    },
    //  删除角色并提醒
    // 页面处理
    jsEs (row) {
      this.$confirm('你确定要删除:' + row.name + '吗?', '提示')
        .then(async () => {
          await sysRoleStae(row.id)
          this.$message.success('删除:' + row.name + '成功')
          //  如果删除的该页只有一条数据且页码>1,页面--
          if (this.page.page > 1 && this.list.length === 1) {
            this.page.page--
          }
          this.getData()
        })
        .catch(() => {})
    },
    //  编辑角色
    esit (row) {
      this.show = true
      this.$refs.add.mode = 'edit'
      //  深拷贝
      this.$refs.add.form = JSON.parse(JSON.stringify(row))
      console.log(row)
    },
    assignClick (id) {
      this.showSet = true
      this.$refs.set.getRoleDetail(id)
    }
  }
}
</script>

<style lang="scss" scoped>
.company {
  padding: 20px;
  .page {
    text-align: right;
    padding: 20px 0;
  }
}
</style>
